<template>
    <h1 style="text-align: center;margin-top: 50px;">患者病床列表</h1>
    <div class="container">
        <div class="patient-list">
            <div 
                v-for="patient in patients" 
                :key="patient.id" 
                class="patient-card"
                @click="goToPatientDetail(patient.id)"
            >
                <div class="card-header">{{ patient.wardCode }}{{ patient.bedCode }}</div>
                <div class="card-body">
                    <div class="patient-name">{{ patient.name }}</div>
                    <div class="patient-info">{{ patient.age }}岁 {{ patient.sex === 1 ? '男' : '女' }}</div>
                    <div class="patient-diagnosis">{{ patient.diagnosis }}</div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { onMounted, ref } from 'vue';
import { useRouter } from 'vue-router';
import {getCurrentInstance} from 'vue';
const { proxy } = getCurrentInstance();
const router = useRouter();
onMounted(()=>{
    getPatientList();
})
// 患者数据
const patients = ref([]);

// 跳转到患者详情页并传递患者编号和名称
const goToPatientDetail = (patientId) => {
    console.log('跳转到患者详情页, ID:', patientId);
    console.log('当前患者列表:', patients.value);
    
    // 查找对应的患者
    const patient = patients.value.find(p => p.id == patientId);
    console.log('找到的患者:', patient);
    
    if (patient) {
        router.push({
            path: "/Echared",
            query: { 
                patientCode: patient.patientCode || ('PAT00' + patientId), // 使用患者编号
                patientName: patient.name || '未知患者'
            }
        });
    } else {
        console.error('未找到ID为', patientId, '的患者');
        // 仍然跳转，但使用默认名称和编号
        router.push({
            path: "/Echared",
            query: { 
                patientCode: 'PAT00' + patientId, // 生成默认编号
                patientName: '未知患者'
            }
        });
    }
}

const getPatientList = () => {
    proxy.http.get('/api/DoctorManageController/GetPatients').then(res=>{
        console.log('获取到的患者列表:', res);
        patients.value = res || [];
        
        // 如果获取失败，添加测试数据
        if (!res || res.length === 0) {
            console.warn('使用测试数据');
            patients.value = [
                {
                    id: 1,
                    name: "张三",
                    wardCode: "WardNum1",
                    bedCode: "1床",
                    sex: 1,
                    age: 35,
                    diagnosis: "上呼吸道感染"
                },
                {
                    id: 2,
                    name: "李芳",
                    wardCode: "WardNum2",
                    bedCode: "2床",
                    sex: 2,
                    age: 52,
                    diagnosis: "Ⅱ型糖尿病"
                }
            ];
        }
    }).catch(err => {
        console.error('获取患者列表失败:', err);
        // 添加测试数据
        patients.value = [
            {
                id: 1,
                name: "张三",
                wardCode: "WardNum1",
                bedCode: "1床",
                sex: 1,
                age: 35,
                diagnosis: "上呼吸道感染"
            },
            {
                id: 2,
                name: "李芳",
                wardCode: "WardNum2",
                bedCode: "2床",
                sex: 2,
                age: 52,
                diagnosis: "Ⅱ型糖尿病"
            }
        ];
    });
}
</script>

<style scoped>
.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
}

.patient-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 20px;
    margin-top: 30px;
}

.patient-card {
    background-color: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    overflow: hidden;
    transition: all 0.3s ease;
    cursor: pointer;
}

.patient-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

.card-header {
    background-color: #409EFF;
    color: white;
    padding: 10px 15px;
    font-weight: bold;
    font-size: 16px;
}

.card-body {
    padding: 15px;
}

.patient-name {
    font-size: 18px;
    font-weight: bold;
    margin-bottom: 5px;
}

.patient-info {
    color: #606266;
    margin-bottom: 5px;
}

.patient-diagnosis {
    color: #F56C6C;
    font-size: 14px;
    margin-top: 10px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
</style>

